<?php
class Menu
{
	public $Table="menu";
	public $MenuID;
	public $ModuleID;
	public $Param;
	public $MenuName;
	public $MenuNameEng;
	public $Summary;
	public $SummaryEng;
	public $MenuDetail;
	public $MenuDetailEng;
	public $MenuParent;
	public $Pos;
	public $Image;
	public $Place;
	public $ListEmail;
	public $MenuUrl;
	public $FileName;
	public $Title;
	public $Direc="../file/menu/";
	
	public $DirectPage="../html/";
	public $DirectPageEng="../htmlEng/";
	
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where MenuID='".$id."'";
		$row=$ob->singleData($sql);
		$this->MenuID=$row['MenuID'];
		$this->ModuleID=$row['ModuleID'];
		$this->Param=$row['Param'];
		$this->MenuName=$row['MenuName'];
		$this->MenuNameEng=$row['MenuNameEng'];
		$this->Summary=$row['Summary'];
		$this->SummaryEng=$row['SummaryEng'];
		$this->MenuDetail=$row['MenuDetail'];
		$this->MenuDetailEng=$row['MenuDetailEng'];
		$this->MenuParent=$row['MenuParent'];
		$this->Pos=$row['Pos'];
		$this->Image=$row['Image'];
		
		$this->ListEmail=$row['ListEmail'];
		$this->MenuUrl=$row['MenuUrl'];
		$this->FileName=$row['FileName'];
		$this->Title=$row['Title'];
	}
	function loadByKeyLang($id,$lang)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where MenuID='".$id."'";
		$row=$ob->singleData($sql);
		$this->MenuID=$row['MenuID'];
		$this->ModuleID=$row['ModuleID'];
		$this->MenuName=$row['MenuName'.$lang];
		$this->Summary=$row['Summary'.$lang];
		$this->MenuDetail=$row['MenuDetail'.$lang];
		$this->MenuParent=$row['MenuParent'];
		$this->Pos=$row['Pos'];
		$this->Image=$row['Image'];
		
		$this->ListEmail=$row['ListEmail'];
		$this->MenuUrl=$row['MenuUrl'];
		$this->FileName=$row['FileName'];
		$this->Title=$row['Title'];
	}
	function getTable($dk)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function listPlace()
	{
		$arr=array("Top","Bottom");
		return $arr;
	}
	
	
	function getListEmail()
	{
		//echo $this->ListEmail;
		$arr = split(",",$this->ListEmail);
		return $arr;
	}
	function insert($conn)
	{
		$ob=new ClassConnect();
		if($this->MenuID=="")
			$this->MenuID=$ob->getNextIdVarChar($this->Table,"MenuID",$this->Table);
		$image=basename( $_FILES['image']['name']);
		if($image!="")
		{
			//Tao thu muc
			$targetbr = $this->Direc;
			if (! is_dir($targetbr))
				mkdir( $targetbr , 0777 );
			$targetbr = $this->Direc.$this->MenuID."/";
			if (! is_dir($targetbr))
				mkdir( $targetbr , 0777 );
			$obupload=new Upload();
			
			$inputName="image";
			$uploadDir=$targetbr;
			$outputName=$this->Table.$this->MenuID;
			$out=$obupload->uploadFile($inputName, $uploadDir,$outputName);
			$this->Image=$out;
		}
		$this->MenuUrl ="menuid=".$this->MenuID;
		$this->FileName = $this->MenuID.".php";		
		$field=array("MenuID","ModuleID","Param","MenuName","MenuNameEng","MenuDetail","MenuDetailEng","MenuParent","Pos","Image","MenuUrl","FileName","Title");
		$value=array($this->MenuID,$this->ModuleID,$this->Param,$this->MenuName,$this->MenuNameEng,$this->MenuDetail,$this->MenuDetailEng,$this->MenuParent,$this->Pos,$this->Image,$this->MenuUrl,$this->FileName,$this->Title);
		$ob->insertData($conn,$this->Table,$field,$value);
		
	}
	
	
	
	
	function update($conn)
	{
		$ob=new ClassConnect();
		$obj = new Menu();
		$obj->loadByKey($this->MenuID);
		
		$image=basename( $_FILES['image']['name']);
		if($image!="")
		{
			$targetbr = $this->Direc;
			if (! is_dir($targetbr))
				mkdir( $targetbr , 0777 );
			$targetbr = $this->Direc.$this->MenuID."/";
			if (! is_dir($targetbr))
				mkdir( $targetbr , 0777 );
			$obupload=new Upload();
			$inputName="image";
			$uploadDir=$targetbr;
			$outputName=$this->Table.$this->MenuID;
			$out=$obupload->uploadFile($inputName, $uploadDir,$outputName);
			$this->Image=$out;
		}
		else
			$this->Image=$obj->Image;
		
		
		$this->FileName=$this->MenuID.".php";		
		$field=array("MenuID","ModuleID","Param","MenuName","MenuNameEng","MenuDetail","MenuDetailEng","MenuParent","Pos","Image","MenuUrl","FileName","Title");
		$value=array($this->MenuID,$this->ModuleID,$this->Param,$this->MenuName,$this->MenuNameEng,$this->MenuDetail,$this->MenuDetailEng,$this->MenuParent,$this->Pos,$this->Image,$this->MenuUrl,$this->FileName,$this->Title);
		$where="MenuID = '".$this->MenuID."'";
		if($this->MenuID!=$this->MenuParent)
		{
			$ob->updateData($conn,$this->Table,$field,$value,$where);
		}
	}
	function updateContent($conn)
	{
		
		$ob=new ClassConnect();
		$obj = new Menu();
		$obj->loadByKey($this->MenuID);
		$field=array("MenuID","Summary","SummaryEng","MenuDetail","MenuDetailEng");
		$value=array($this->MenuID,$this->Summary,$this->SummaryEng,$this->MenuDetail,$this->MenuDetailEng);
		$where="MenuID = '".$this->MenuID."'";
		$ob->updateData($conn,$this->Table,$field,$value,$where);
		
	}
	function updateListEmail($conn)
	{
		$ob=new ClassConnect();
		$obj = new Menu();
		$obj->loadByKey($this->MenuID);
		$field=array("MenuID","ListEmail");
		$value=array($this->MenuID,$this->ListEmail);
		$where="MenuID = '".$this->MenuID."'";
		$ob->updateData($conn,$this->Table,$field,$value,$where);
		
	}
	function updateParam($conn)
	{
		$ob=new ClassConnect();
		$obj = new Menu();
		$obj->loadByKey($this->MenuID);
		$field=array("MenuID","Param");
		$value=array($this->MenuID,$this->Param);
		$where="MenuID = '".$this->MenuID."'";
		$ob->updateData($conn,$this->Table,$field,$value,$where);
		
	}
	function delete($conn)
	{
		$ob=new ClassConnect();
		$listChid=$this->getListChil($this->MenuID);
		$targetbr = $this->Direc.$this->MenuID."/";
		
		$this->loadByKey($this->MenuID);
		if(count($listChid)==0)
		{
			$where="MenuID = '".$this->MenuID."'";
			return $ob->deleteData($conn,$this->Table,$where);
		}
			
	}
	function deleteList($conn,$list)
	{
		$maxdeep=$this->getMaxDeep($list);
		$obj=new Menu();
		for($i=$maxdeep;$i>=0;$i--)
		{
			foreach($list as $val)
			{
				if($obj->deep($val)==$i)
				{	
					$obj->MenuID=$val;
					$obj->delete($conn);
				}	
			}
		}
	}
	function updatePos($conn)
	{
		$ob=new ClassConnect();
		$field=array("MenuID","Pos");
		$value=array($this->MenuID,$this->Pos);
		$where="MenuID ='".$this->MenuID."'";
		
		$ob->updateData($conn,$this->Table,$field,$value,$where);
	}
	function getListChil($id)
	{
		$data=$this->getTable(" WHERE MenuParent='$id' ORDER BY Pos");
		$arr=array();
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['MenuID']);
		return $arr;
	}
	function getChil()
	{
		$data=$this->getTable(" WHERE MenuParent='".$this->MenuID."' ORDER BY Pos");
		$arr=array();
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['MenuID']);
		return $arr;
	}
	function isLastChid()
	{
		$maxpost=$this->getNextPos($this->MenuParent);
		if($this->Pos == $maxpost-1)
			return true;
		else 
			return false;
	}
	function listType()
	{
		$arr=array(
					"group"=>"Group",
					"homepage"=>"Home page",
					"content"=>"Content",
					"post"=>"List",
					
					"link"=>"Link",
					"rss"=>"RSS ",
					"contact"=>"Contact",
					"product"=>"Products",
					"download"=>"Down load",
					"banner" => "Đối tác"
				);
		return $arr;
	}
	
	function getType()
	{
		$arr=$this->listType();
		return $arr[$this->ModuleID];
	}
	function cbListType($cbname,$selectValue)
	{
		$arr=$this->listType();
		$cb="<select name='$cbname'>";
		foreach($arr as $key => $val)
		{
			$sel="";
			if($key == $selectValue)
				$sel="selected";
			$cb.="<option value='".$key."' $sel>".$val."</option>";
		}
		$cb.="</select>";
		return $cb;
	}
	function getNextPos($cparent)
	{
		$ob=new ClassConnect();
		$sql="SELECT max(Pos) as max FROM `menu` where MenuParent='$cparent'";
		$row=$ob->singleData($sql);
		$max=$row['max'];
		return $max + 1;
	}
	function deep($id)
	{
		if($id=="")
			return 0;
		$count=0;
		$obj=new Menu();
		$obj->loadByKey($id);
		
		while($obj->MenuParent!="")
		{
			$count++;
			$obj->loadByKey($obj->MenuParent);
		}
		return $count;
	}
	function getMaxDeep($listid)
	{
		$max=0;
		$obj=new Menu();
		foreach($listid as $val)
		{
			if($obj->deep($val)>$max)
				$max=$obj->deep($val);
		}
		return $max;
	}
	function getListtoRoot($id,$root="")
	{
		$arr=array();
		while($id!="" && $id != $root)
		{
			$this->loadByKey($id);
			array_push($arr,$id);
			$id=$this->MenuParent;
		}
		return $arr;
	}
	
	function getPath($root,$str,$lang="")
	{
		$obj = new Menu();
		
		$link = array();
		$arr = $obj->getListtoRoot($this->MenuID,$root);
		
		
		for($i=count($arr)-1;$i>=0;$i--)
		{
			$obj->loadByKeyLang($arr[$i],$lang);
			$link[]= "<a href='".$this->createLink($obj,$lang)."'>".$obj->MenuName."</a>";	
		}
		
		return implode($str,$link);
	}
	
	function travel($id,&$list)
	{
		array_push($list,$id);
		$arr=$this->getListChil($id);
		if(count($arr)>0)
			for($i=0;$i<count($arr);$i++)
				$this->travel($arr[$i],$list);
		else
			return;
		
	}
	function pathNodeID()
	{
		$obj=new Menu();
		$arr = $obj->getListtoRoot($this->MenuID);
		$s="";
		
		for($i=count($arr)-1;$i>=0;$i--)
		{
			$s.="$arr[$i]";
		}
		return $s;
	}
	
	function showDeep($id,$letter)
	{
		$deep=$this->deep($id);
		$s="";
		for($i=0;$i<$deep;$i++)
			$s=$s.$letter;
		return $s;
	}
	function cbMenu($cbname,$id,$selectvalue)
	{
		$arr=array();
		$this->travel($id,$arr);
		unset($arr[0]);
		$cb="<select name='$cbname'>";
		$cb.="<option value=''></option>";
		foreach($arr as $item)
		{
			$obj=new Menu();
			$obj->loadByKey($item);
			$sel="";
			if($item==$selectvalue)
				$sel="selected";
			$cb.="<option value='".$obj->MenuID."' $sel>".$this->showDeep($obj->MenuID,'&nbsp;&nbsp;&nbsp;').$obj->MenuName."</option>";
		}
		$cb.="</select>";
		return $cb;
	}
	function genXMLString($dk,$pos)
	{
		$obj=new Menu();
		$data=array();
		$obj->travel($dk,$data);
		$xml='<?xml version="1.0"?>
<menu name="menu2" orient = "h">
			';
		for($i=1;$i<count($data);$i++)
		{
			$obj->loadByKey($data[$i]);
			if($pos==$obj->Pos)
				$sel="True";
			else
				$sel="False";
			$xml.='<item name="'.$obj->MenuName.'" action="url" value="?module='.$obj->Table.'&id='.$obj->MenuID.'" selected="'.$sel.'"/>
			';
		}
				
		$xml.='		
</menu>';
		return $xml;
	}
	function showtreeNoRewrite($id,$lang = "")
	{
		$obj=new Menu();
		$obj->loadByKeyLang($id,$lang);
		$listchid=$this->getListChil($id);
		if($id!="")
		{	
			if(count($listchid)==0)
				echo "<li><a href='?menu=".$obj->MenuID."'>".$obj->MenuName."</a></li>";
			else
				echo "<li><a href='?menu=".$obj->MenuID."'>".$obj->MenuName."</a>";
		}
		
		if(count($listchid)>0)
		{
			echo "<ul>";
			for($i=0;$i<count($listchid);$i++)
			{
				$this->showtreeNoRewrite($listchid[$i],$lang);
			}
			echo "</ul>";
			if($id!="")
				echo "</li>";
		}	
		
	}
	
	function createLink($obj,$lang)
	{
		if($lang=="")
			$site = "vn";
		else
			$site = $lang;
			
		$link = "";
		switch($obj->ModuleID)
		{
			case "homepage"	:
				$link = HTTP_SERVER;
			break;
			
			default:
				
				$link = HTTP_SERVER."site/".$site."/".$obj->MenuID."/";
				
		}
		return $link;
	}
	
	function showtree($id,$lang = "")
	{
		$obj=new Menu();
		$obj->loadByKeyLang($id,$lang);
		$listchid=$this->getListChil($id);
		if($id!="")
		{	
			
			
			
			if(count($listchid)==0)
				echo "<li><a href='".$this->createLink($obj,$lang)."'>".$obj->MenuName."</a></li>";
			else
				echo "<li><a>".$obj->MenuName."</a></a>";
			
		}
		
		if(count($listchid)>0)
		{
			echo "<ul>";
			for($i=0;$i<count($listchid);$i++)
			{
				$this->showtree($listchid[$i],$lang);
			}
			echo "</ul>";
			if($id!="")
				echo "</li>";
		}	
		
	}
	
	function showMenu($id,$lang="")
	{
		$obj=new Menu();
		$listchid=$this->getListChil($id);
		foreach($listchid as $item)
		{
			
				$this->showtree($item,$lang);
		}
	}
	

	
	public function getMenu($parentid,$lang)
	{
		
		$str = "";
		
		$sitemaps = $this->getListChil($parentid);
		
		foreach($sitemaps as $item)
		{
			$childs = $this->getListChil($item);
			$obj = new Menu();
			$obj->loadByKeyLang($item,$lang);
			/*$link = "<a class='left'>".$item['sitemapname']."</a>";
			
			if(substr($item['moduleid'],0,6) == "group/")
			{
				$item['moduleid'] = "module/information";
			}
			
			
			if($item['moduleid'] != "group" && $item['moduleid'] != "homepage")
			{
				//$link='<a class="left" href="?route='.$item['moduleid']."&sitemapid=".$item['sitemapid'].'" title="[Detail]">'.$item['sitemapname'].'</a>';
				$link = "<a  href='".HTTP_SERVER."site/".$siteid."/".$item['sitemapid']."'>".$item['sitemapname']."</a>";
			}*/
			if($lang=="")
				$site = "vn";
			else
				$site = $lang;
			$link = "<a  href='".HTTP_SERVER."site/".$site."/".$obj->MenuID."'>".$obj->MenuName."</a>";
			$str .= "<li>";
			$str .= "<div class='collape'>";
			$str .= $link;
			
			if(count($childs) > 0)
			{
				$str .= "<span class='collape right'>[+]</span>";
				$str .= '<div class="clearer">&nbsp;</div>';
				$str .= "</div>";
				
				$str .= "<ul id='".$parentid."'>";
				$str .= $this->getMenu($obj->MenuID,$lang);
				$str .= "</ul>";
			}
			else
			{
				$str .= '<div class="clearer">^&nbsp;</div>';
				$str .= "</div>";
				
			}
			$str .= "</li>";
		}
		
		return $str;
		
	}
}
?>
